package cn.sdut.training.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.sdut.training.dao.BaseDao;
import cn.sdut.training.domain.User;
import cn.sdut.training.domain.UserAccount;
import cn.sdut.training.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource
	private BaseDao<User> userDao ;
	@Resource
	private BaseDao<UserAccount> userAccountDao;
	
	@Override
	public User getUserByUsernameAndPassword(String userName, String password) {
		return (User) userDao.uniqueResult("from User u where u.loginName = ? and u.password = ?", userName,password);
	}

	@Override
	public UserAccount getUserAccountById(Long userAccountId) {
		return userAccountDao.getEntity(userAccountId);
	}

	@Override
	public List<UserAccount> getUserAccountListByUserId(Long id) {
		return userAccountDao.findEntityByHQL("from UserAccount a where a.user.id = ?", id);
	}

	@Override
	public void saveUser(User user) {
		userDao.saveEntity(user);
	}

	@Override
	public void deleteUserAccount(Long id) {
		userAccountDao.deleteEntity(userAccountDao.getEntity(id));
	}

	@Override
	public void saveUserAccount(UserAccount userAccount) {
		userAccountDao.saveEntity(userAccount);
	}

	@Override
	public void updateUserAccount(UserAccount userAccount) {
		userAccountDao.updateEntity(userAccount);
	}

	@Override
	public List<User> findAllUser() {
		return userDao.findEntityByHQL("from User");
	}

	@Override
	public void deleteUser(Long id) {
		userDao.deleteEntity(userDao.getEntity(id));
	}

	@Override
	public User getUserById(Long id) {
		return userDao.getEntity(id);
	}

	@Override
	public void updateUser(User user) {
		userDao.updateEntity(user);
	}
	
}
